import {
	isHidden,
	isPrimaryKey,
	isRelational,
	isDateUpdated,
	isDateCreated,
	isUserUpdated,
	isUserCreated,
} from '@/utils/field-utils';
import type { Field } from '@directus/types';
import { expect, test } from 'vitest';

test('isPrimaryKey', () => {
	const field = fields.find((f) => f.field === 'id');
	const wrongField = fields.find((f) => f.field === 'm2o');

	expect(isPrimaryKey(field!)).toBe(true);
	expect(isPrimaryKey(wrongField!)).toBe(false);
});

test('isHidden', () => {
	const field = fields.find((f) => f.field === 'created_by');
	const wrongField = fields.find((f) => f.field === 'm2o');

	expect(isHidden(field!)).toBe(true);
	expect(isHidden(wrongField!)).toBe(false);
});

test('isRelational', () => {
	const m2oField = fields.find((f) => f.field === 'm2o');
	const o2mField = fields.find((f) => f.field === 'o2m');
	const m2mField = fields.find((f) => f.field === 'm2m');
	const m2aField = fields.find((f) => f.field === 'm2a');
	const wrongField = fields.find((f) => f.field === 'id');

	expect(isRelational(m2oField!)).toBe(true);
	expect(isRelational(o2mField!)).toBe(true);
	expect(isRelational(m2mField!)).toBe(true);
	expect(isRelational(m2aField!)).toBe(true);
	expect(isRelational(wrongField!)).toBe(false);
});

test('isDateUpdated', () => {
	const field = fields.find((f) => f.field === 'updated_at');
	const wrongField = fields.find((f) => f.field === 'id');

	expect(isDateUpdated(field!)).toBe(true);
	expect(isDateUpdated(wrongField!)).toBe(false);
});

test('isDateCreated', () => {
	const field = fields.find((f) => f.field === 'created_at');
	const wrongField = fields.find((f) => f.field === 'id');

	expect(isDateCreated(field!)).toBe(true);
	expect(isDateCreated(wrongField!)).toBe(false);
});

test('isUserUpdated', () => {
	const field = fields.find((f) => f.field === 'updated_by');
	const wrongField = fields.find((f) => f.field === 'updated_at');

	expect(isUserUpdated(field!)).toBe(true);
	expect(isUserUpdated(wrongField!)).toBe(false);
});

test('isUserCreated', () => {
	const field = fields.find((f) => f.field === 'created_by');
	const wrongField = fields.find((f) => f.field === 'm2a');

	expect(isUserCreated(field!)).toBe(true);
	expect(isUserCreated(wrongField!)).toBe(false);
});

const fields: Field[] = [
	{
		collection: 'test_collection',
		field: 'id',
		type: 'integer',
		schema: {
			name: 'id',
			table: 'test_collection',
			data_type: 'integer',
			default_value: null,
			max_length: null,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: false,
			is_unique: false,
			is_indexed: false,
			is_primary_key: true,
			has_auto_increment: true,
			foreign_key_column: null,
			foreign_key_table: null,
		},
		meta: {
			id: 192,
			collection: 'test_collection',
			field: 'id',
			special: null,
			interface: 'input',
			options: null,
			display: null,
			display_options: null,
			readonly: true,
			hidden: true,
			sort: 1,
			width: 'full',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'ID',
	},
	{
		collection: 'test_collection',
		field: 'created_by',
		type: 'string',
		schema: {
			name: 'created_by',
			table: 'test_collection',
			data_type: 'char',
			default_value: null,
			max_length: 36,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: 'id',
			foreign_key_table: 'directus_users',
		},
		meta: {
			id: 193,
			collection: 'test_collection',
			field: 'created_by',
			special: ['user-created'],
			interface: 'select-dropdown-m2o',
			options: {
				template: '{{avatar}} {{first_name}} {{last_name}}',
			},
			display: 'user',
			display_options: null,
			readonly: true,
			hidden: true,
			sort: 2,
			width: 'half',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'Created By',
	},
	{
		collection: 'test_collection',
		field: 'created_at',
		type: 'timestamp',
		schema: {
			name: 'created_at',
			table: 'test_collection',
			data_type: 'datetime',
			default_value: null,
			max_length: null,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: null,
			foreign_key_table: null,
		},
		meta: {
			id: 194,
			collection: 'test_collection',
			field: 'created_at',
			special: ['date-created', 'cast-timestamp'],
			interface: 'datetime',
			options: null,
			display: 'datetime',
			display_options: {
				relative: true,
			},
			readonly: true,
			hidden: true,
			sort: 3,
			width: 'half',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'Created At',
	},
	{
		collection: 'test_collection',
		field: 'updated_by',
		type: 'string',
		schema: {
			name: 'updated_by',
			table: 'test_collection',
			data_type: 'char',
			default_value: null,
			max_length: 36,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: 'id',
			foreign_key_table: 'directus_users',
		},
		meta: {
			id: 195,
			collection: 'test_collection',
			field: 'updated_by',
			special: ['user-updated'],
			interface: 'select-dropdown-m2o',
			options: {
				template: '{{avatar}} {{first_name}} {{last_name}}',
			},
			display: 'user',
			display_options: null,
			readonly: true,
			hidden: true,
			sort: 4,
			width: 'half',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'Updated By',
	},
	{
		collection: 'test_collection',
		field: 'updated_at',
		type: 'timestamp',
		schema: {
			name: 'updated_at',
			table: 'test_collection',
			data_type: 'datetime',
			default_value: null,
			max_length: null,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: null,
			foreign_key_table: null,
		},
		meta: {
			id: 196,
			collection: 'test_collection',
			field: 'updated_at',
			special: ['date-updated', 'cast-timestamp'],
			interface: 'datetime',
			options: null,
			display: 'datetime',
			display_options: {
				relative: true,
			},
			readonly: true,
			hidden: true,
			sort: 5,
			width: 'half',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'Updated At',
	},
	{
		collection: 'test_collection',
		field: 'title',
		type: 'string',
		schema: {
			name: 'title',
			table: 'test_collection',
			data_type: 'varchar',
			default_value: null,
			max_length: 255,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: null,
			foreign_key_table: null,
		},
		meta: {
			id: 197,
			collection: 'test_collection',
			field: 'title',
			special: null,
			interface: 'input',
			options: null,
			display: null,
			display_options: null,
			readonly: false,
			hidden: false,
			sort: 6,
			width: 'full',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'Title',
	},
	{
		collection: 'test_collection',
		field: 'description',
		type: 'text',
		schema: {
			name: 'description',
			table: 'test_collection',
			data_type: 'text',
			default_value: null,
			max_length: null,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: null,
			foreign_key_table: null,
		},
		meta: {
			id: 198,
			collection: 'test_collection',
			field: 'description',
			special: null,
			interface: 'input-multiline',
			options: null,
			display: null,
			display_options: null,
			readonly: false,
			hidden: false,
			sort: 7,
			width: 'half',
			translations: null,
			note: null,
			conditions: [
				{
					name: 'Hide if toggle is enabled',
					rule: {
						_and: [
							{
								hide_description: {
									_eq: true,
								},
							},
						],
					},
					hidden: false,
				},
			],
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'Description',
	},
	{
		collection: 'test_collection',
		field: 'hide_description',
		type: 'boolean',
		schema: {
			name: 'hide_description',
			table: 'test_collection',
			data_type: 'boolean',
			default_value: false,
			max_length: null,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: false,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: null,
			foreign_key_table: null,
		},
		meta: {
			id: 199,
			collection: 'test_collection',
			field: 'hide_description',
			special: ['cast-boolean'],
			interface: 'boolean',
			options: null,
			display: null,
			display_options: null,
			readonly: false,
			hidden: false,
			sort: 8,
			width: 'half',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'Hide Description',
	},
	{
		collection: 'test_collection',
		field: 'm2o',
		type: 'integer',
		schema: {
			name: 'm2o',
			table: 'test_collection',
			data_type: 'integer',
			default_value: null,
			max_length: null,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: 'id',
			foreign_key_table: 'test_collection',
		},
		meta: {
			id: 200,
			collection: 'test_collection',
			field: 'm2o',
			special: ['m2o'],
			interface: 'select-dropdown-m2o',
			options: {
				template: '{{title}}',
			},
			display: null,
			display_options: null,
			readonly: false,
			hidden: false,
			sort: 9,
			width: 'full',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'M2O',
	},
	{
		collection: 'test_collection',
		field: 'o2m',
		type: 'alias',
		schema: null,
		meta: {
			id: 201,
			collection: 'test_collection',
			field: 'o2m',
			special: ['o2m'],
			interface: 'list-o2m',
			options: {
				sort: null,
				template: '{{title}}',
			},
			display: null,
			display_options: null,
			readonly: false,
			hidden: false,
			sort: 10,
			width: 'full',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'O2M',
	},
	{
		collection: 'test_collection',
		field: 'o2m_parent_id',
		type: 'integer',
		schema: {
			name: 'o2m_parent_id',
			table: 'test_collection',
			data_type: 'integer',
			default_value: null,
			max_length: null,
			numeric_precision: null,
			numeric_scale: null,
			is_generated: false,
			generation_expression: null,
			is_nullable: true,
			is_unique: false,
			is_indexed: false,
			is_primary_key: false,
			has_auto_increment: false,
			foreign_key_column: 'id',
			foreign_key_table: 'test_collection',
		},
		meta: {
			id: 202,
			collection: 'test_collection',
			field: 'o2m_parent_id',
			special: null,
			interface: 'select-dropdown-m2o',
			options: null,
			display: null,
			display_options: null,
			readonly: false,
			hidden: true,
			sort: 11,
			width: 'half',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'O2M Parent ID',
	},
	{
		collection: 'test_collection',
		field: 'm2m',
		type: 'alias',
		schema: null,
		meta: {
			id: 203,
			collection: 'test_collection',
			field: 'm2m',
			special: ['m2m'],
			interface: 'list-m2m',
			options: {
				layout: 'table',
				fields: ['test_collection_id.title'],
			},
			display: null,
			display_options: null,
			readonly: false,
			hidden: false,
			sort: 12,
			width: 'full',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'M2M',
	},
	{
		collection: 'test_collection',
		field: 'm2a',
		type: 'alias',
		schema: null,
		meta: {
			id: 207,
			collection: 'test_collection',
			field: 'm2a',
			special: ['m2a'],
			interface: 'list-m2a',
			options: null,
			display: null,
			display_options: null,
			readonly: false,
			hidden: false,
			sort: 13,
			width: 'full',
			translations: null,
			note: null,
			conditions: null,
			required: false,
			group: null,
			validation: null,
			validation_message: null,
		},
		name: 'M2A',
	},
];
